package com.ricchieri.manage.bean;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.lazymodel.UserLazyDataModel;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IDocumentTypeService;
import com.ricchieri.service.interf.IUserService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "searchOperationMB")
@ViewScoped
public class SearchOperationManageBean extends GenericManageBean {
	private static final long serialVersionUID = -3337969920585553310L;
	
	@ManagedProperty(value = "#{UserService}")
	IUserService userService;
	
	@ManagedProperty(value = "#{DocumentTypeService}")
	IDocumentTypeService documentTypeService;
	
	private UserFilter userFilter = new UserFilter();
	
	private UserLazyDataModel userLazyDataModel; 
	
	private Map<String, String> documents;
	
	@PostConstruct
	public void init() {
		userFilter = (UserFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(userFilter == null) {
			userFilter = new UserFilter();
		}
	}

	// Se encarga de la busqueda.
	public UserLazyDataModel getUserLazyDataModel() {
		if(userLazyDataModel == null){
			userLazyDataModel = new UserLazyDataModel(userService, userFilter);
		}
		
		return userLazyDataModel;
	}

	public void setUserLazyDataModel(UserLazyDataModel userLazyDataModel) {
		this.userLazyDataModel = userLazyDataModel;
	}

	public IUserService getUserService() {
		return userService;
	}

	public UserFilter getUserFilter() {
		userFilter.setRole(BusinessSettings.ROLE_PASSANGER);
		
		return userFilter;
	}

	public void setUserFilter(UserFilter userFilter) {
		this.userFilter = userFilter;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IDocumentTypeService getDocumentTypeService() {
		return documentTypeService;
	}

	public void setDocumentTypeService(IDocumentTypeService documentTypeService) {
		this.documentTypeService = documentTypeService;
	}
	
	public Map<String, String> getDocuments() {
		if(documents == null){
			documents = getDocumentTypeService().getComboDocuments(); 
		}
		
		return documents;
	}

	public void setDocuments(Map<String, String> documents) {
		this.documents = documents;
	}

	public void search(){
		userLazyDataModel = null;
	}
	
	public void reset() {
		userFilter = new UserFilter();
		userLazyDataModel = null;
	}
	
	public String viewDetails(User user){
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, getUserFilter());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, user);
		return BusinessSettings.VIEW_DETAILS;
	}
	
	public String addOperation(User user){
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, getUserFilter());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, user);
		
		return BusinessSettings.ADD;
	}
}

